dojo.provide("velcro.item");

dojo.require("dojo.lang.declare");
dojo.require("lotex.string");
dojo.require("velcro.dao");

dojo.declare("velcro.item.Item", null,
    function(objname) {
        this._name = objname;
    },
{
    _id: null,
    _checkExistSql: "",
    _saveUrl: "",
    _postContent: "",

    setName: function(objname) {
        if(this._name == objname) return;
        this._name = objname;
        this._id = null;
    },

    setPid: function(pid) {
        this._pid = pid;
    },

    setTypeid: function(typeid) {
        this._typeid = typeid;
    },

    setDesc: function(desc) {
        this._desc = desc;
    },

    setPostContent: function (content) {
        this._postContent = content;
    },

    getCheckExistSql: function () {
        return this._checkExistSql;
    },

    getPostContent: function () {
        return this._postContent;
    },

    getId: function() {
        return lotex.string.isBlank(this._id) ? this.getIdFromDb() : this._id;
    },

    getIdFromDb: function() {
        var sql = this.getCheckExistSql();
d("check exist : ",sql);
        var id = velcro.dao.find(sql);
//d(xml);
        this._id = id;

        return id;
    },

    isExist: function() {
        return lotex.string.isBlank(this.getId()) ? false : true;
    },

    // 如果设定overwite为true，则即使数据库已有同名项目，也会添加改项目！
    save: function(overwite) {
        if (this.isExist() && !overwite) return this.getId();

        var content = this.getPostContent();
        d("post: ",content);
        var kw = {
            sync: true,
            url: lotexConfig.baseurl + this._saveUrl,
            postContent: content,
            method: "post",
            mimetype: "text/plain",
            load: function(type, data, evt) {
                d("OK");
            },
            error: function(t, e) {
                alert("Error!");
                d(t, e);
            }
        };
        dojo.io.bind(kw);
        return this.getId();
    }
});

dojo.declare("velcro.item.SelectItemType", velcro.item.Item, null, {
    //_checkExistSql: "SELECT id FROM selectitemtype"
    //    + " WHERE (objname = '资产管理') AND (pid IS NULL)",

    _saveUrl: "/ServiceAction/com.velcro.base.selectitem"
        + ".servlet.SelectitemtypeAction?action=create",

    getCheckExistSql: function () {
        return "SELECT id FROM selectitemtype"
            + " WHERE (objname = '" + this._name + "') AND (pid IS NULL)";
    },

    getPostContent: function () {
        return "objname=" + this._name + "&dsporder=1";
    }
});

dojo.declare("velcro.item.SelectItem", velcro.item.Item, null, {
    _saveUrl: "/ServiceAction/com.velcro.base.selectitem.servlet.SelectitemtypeAction?action=create",

    getCheckExistSql: function () {
/*
SELECT id
FROM selectitemtype
WHERE (objname = '资产计量单位') AND (pid = '402881e50dbeb8a3010dbec47a7b0013')
*/
        return "SELECT id FROM selectitemtype"
            + " WHERE (objname = '" + this._name + "') AND (pid = '"
            + this._pid + "')";
    },

    getPostContent: function () {
        return "objname=" + this._name
            + "&pid=" + this._pid
            + "&dsporder=1";
    }
});

dojo.declare("velcro.item.SelectItemNode", velcro.item.Item, null, {
    //_checkExistSql: "SELECT id FROM selectitemtype"
    //    + " WHERE (objname = '资产管理') AND (pid IS NULL)",

    _saveUrl: "/ServiceAction/com.velcro.base.selectitem"
        + ".servlet.SelectitemAction?action=crud",

    getCheckExistSql: function () {
        return "SELECT id FROM selectitem"
            + " WHERE (objname = '" + this._name + "') AND (typeid = '"
            + this._typeid + "')";
    },

    getPostContent: function () {
        var content = "searchName=&mode=&typeid=" + this._typeid
            + "&delids=&ids=&pid="
            + "&objname=" + this._name
            + "&objdesc=" + this._desc
            + "&dsporder=1&istrue=&id=";
        if (this.getId() != null) content += this.getId();
        return content;
    }
});
